package com.hzit.common.utils;

import cn.hutool.core.lang.hash.Hash;
import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.shaded.com.google.gson.Gson;
import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.service.dysmsapi20170525.AsyncClient;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsResponse;
import darabonba.core.client.ClientOverrideConfiguration;


import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * 类名：
 * 作者：WF
 * 功能：发送手机验证码
 */
public class SmsUtil {

	public void sendMsg(String accessKeyId, String accessKeySecret, String phone, String signName, String templateCode, String code) {
		AsyncClient client = null;
		SendSmsResponse resp = null;
		try {
			StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
					.accessKeyId(accessKeyId)
					.accessKeySecret(accessKeySecret)
					.build());
			Map map = new HashMap<>();
			map.put("code", code);
			client = AsyncClient.builder()
					.region("cn-hangzhou") // Region ID
					.credentialsProvider(provider)
					.overrideConfiguration(
							ClientOverrideConfiguration.create()
									.setEndpointOverride("dysmsapi.aliyuncs.com")
					)
					.build();

			// Parameter settings for API request
			SendSmsRequest sendSmsRequest = SendSmsRequest.builder()
					.phoneNumbers(phone)
					.signName(signName)
					.templateCode(templateCode)
					.templateParam(JSON.toJSONString(map))
					.build();

			CompletableFuture<SendSmsResponse> response = client.sendSms(sendSmsRequest);


			resp = response.get();
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			System.out.println(new Gson().toJson(resp));
			client.close();
		}


	}
}
